<?php

namespace app\admin\controller;

use app\admin\model\Cash;
use think\Db;
use think\Request;

class CashController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }

    // 充值记录
    public function read()
    {
        $cash = new Cash();
        $cash = $cash->field("FROM_UNIXTIME(c.add_time,'yyyy-MM-dd') as add_time,FROM_UNIXTIME(c.examine_time,'yyyy-MM-dd') as examine_time ,c.*");
        $cash = $cash->field("m.phone,m.nickname");
        $cash = $cash->field("d.admin_nickname");
        $cash = $cash->alias('c');
        $cash = $cash->leftJoin('member m', 'c.member_id=m.member_id');
        $cash = $cash->leftJoin('admin_user d', 'd.admin_id=c.admin_id');
        $count = $cash->count();
        $list = $cash->page(input('get.page', input('get.limit')))
            ->order('cash_id', 'desc')
            ->select();
        return json(array('code' => 0, 'msg' => "", 'data' => $list, 'count' => $count));
    }

    public function examine(Request $request)
    {
        $cash = new Cash();
        $admin_id =session('userinfo')['admin_id'];
        $data = $request->post();
        $data['admin_id'] = $admin_id;
        $data['examine_time'] =time();
        if (!isset($data['status'])){
            $cash_data = $cash->isUpdate(true)->save([
                'remark'=>$data['remark'],
                'cash_state'=>3,
                'cash_id'=>$data['cash_id'],
                'admin_id'=>$admin_id,
                'examine_time'=>time()
            ]);
            if ($cash_data){
                return json(array('code' => 0, 'msg' => "更新状态"));
            }else{
                return json(array('code' => 4004, 'msg' => "录入失败"));
            }
            die;
        }
        $cash = new Cash();
        $back_cash = $this->transfer(time(),$data['account'],$data['name'],$data['cash_money'],'豌豆电商');
        if ($back_cash){
            $cash_data = $cash->isUpdate(true)->save($data);
            if (!$cash_data){
                return json(array('code' => 404, 'msg' => "更新状态失败"));
            }
            return json(array('code' => 0, 'msg' => "转账成功"));
        }else{
            $data['cash_sate'] =3;
            $cash_data = $cash->isUpdate(true)->save($data);
            return json(array('code' => 404, 'msg' => "账户名有误！或者用户名正确转账失败"));
        }

    }

    /**
     * @param $order_number
     * @param $pay_no
     * @param $pay_name
     * @param $amount
     * @param $memo
     * @throws \Exception
     */

    private  function transfer($order_number, $pay_no, $pay_name, $amount, $memo)
    {
        //存入转账日志
        include_once \think\facade\Env::get('EXTEND_PATH') . 'alipayqr/aop/AopClient.php';
        include_once \think\facade\Env::get('EXTEND_PATH') . 'alipayqr/aop/request/AlipayFundTransToaccountTransferRequest.php';
        $alipay_config = config('alipay.');
        $aopObj = new \AopClient ();
        //设置值
        $aopObj->gatewayUrl = $alipay_config['gatewayUrl'];
        $aopObj->appId      = $alipay_config['app_id'];
        $aopObj->rsaPrivateKey =$alipay_config['merchant_private_key'];
        $aopObj->alipayrsaPublicKey = $alipay_config['alipay_public_key'];
        $aopObj->apiVersion = '1.0';
        $aopObj->postCharset =  $alipay_config['charset'];
        $aopObj->format = 'json';
        $aopObj->signType = $alipay_config['sign_type'];
        $request = new \AlipayFundTransToaccountTransferRequest ();
        $request->setBizContent("{" .
            "\"out_biz_no\":\"".$order_number."\"," .//商户生成订单号
            "\"payee_type\":\"ALIPAY_LOGONID\"," .//收款方支付宝账号类型
            "\"payee_account\":\"".$pay_no."\"," .//收款方账号
            "\"amount\":\"".$amount."\"," .//总金额
            "\"payer_show_name\":\"豌豆\"," .//付款方账户
            "\"payee_real_name\":\"".$pay_name."\"," .//收款方姓名
            "\"remark\":\"".$memo."\"" .//转账备注
            "}");
        $result = $aopObj->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
           return true;
        } else {
            return false;
        }

    }

}
